package com.example.music.config;

import com.example.music.tools.Constant;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MusicInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        //登录的时候，登录成功的话会创建 session，并且在在里面设置用户属性
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(Constant.USERINFO_SESSION_KEY) != null) {
            return true;//表示用户通过验证，用户处于已登录状态
        }
        //重定向
        System.out.println("用户未登录");
        return false;//表示 用户未登录
    }
}
